package other.edan.system.utils;

import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventResponse;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEventResponse;

/**
 * ClassName: ResultBody
 * Description: 统一数据返回格式
 * date: 2023/03/23
 *
 * @author 尹有仁
 */
public class ResultBody {
    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应结果
     */
    private Object result;

    public ResultBody() {
    }

    public ResultBody(ResultEnum errorInfo) {
        this.code = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    /**
     * 成功
     * 默认返回 "操作成功" 的提示
     *
     * @return
     */
    public static ResultBody success() {
        return success(ResultEnum.SUCCESS.getResultMsg());
    }

    /**
     * 成功
     *
     * @param message 返回的结果消息
     * @return
     */
    public static ResultBody success(String message, Object data) {
        ResultBody rb = new ResultBody();
        rb.setCode(ResultEnum.SUCCESS.getResultCode());
        rb.setMessage(message);
        rb.setResult(data);
        return rb;
    }

    /**
     * 成功
     *
     * @param data 返回的数据对象
     * @return
     */
    public static ResultBody success(Object data) {
        ResultBody rb = new ResultBody();
        rb.setCode(ResultEnum.SUCCESS.getResultCode());
        rb.setMessage(ResultEnum.SUCCESS.getResultMsg());
        rb.setResult(data);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(ResultEnum errorInfo) {
        ResultBody rb = new ResultBody();
        rb.setCode(errorInfo.getResultCode());
        rb.setMessage(errorInfo.getResultMsg());
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(String code, String message) {
        ResultBody rb = new ResultBody();
        rb.setCode(code);
        rb.setMessage(message);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(String message) {
        ResultBody rb = new ResultBody();
        rb.setCode("-1");
        rb.setMessage(message);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    /**
     * 审批流返回结果
     *
     * @param b
     * @param msg
     * @return
     */
    public static ApprovalEventResponse approvalResponse(Boolean b, String msg) {
        ApprovalEventResponse response = new ApprovalEventResponse();
        response.setSuccess(b);
        response.setMsg(msg);
        return response;
    }

    /**
     * 定时调度、触发规则返回结果
     *
     * @param b
     * @param msg
     * @return
     */
    public static RuleEventResponse ruleEventResponse(Boolean b, String msg) {
        RuleEventResponse response = new RuleEventResponse();
        response.setSuccess(b);
        response.setMsg(msg);
        return response;
    }

    /**
     * ClassName: ResultEnum
     * Description: 错误定义枚举类
     * date: 2023/03/23
     *
     * @author 尹有仁
     */
    public enum ResultEnum {
        SUCCESS("200", "操作成功!"),
        SYNC_FAILE("1001", "同步失败!"),
        ;

        /**
         * 错误码
         */
        private String resultCode;

        /**
         * 错误描述
         */
        private String resultMsg;

        ResultEnum(String resultCode, String resultMsg) {
            this.resultCode = resultCode;
            this.resultMsg = resultMsg;
        }

        public String getResultCode() {
            return resultCode;
        }

        public String getResultMsg() {
            return resultMsg;
        }
    }
}
